package me.yobol.greentable.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.yobol.greentable.bean.DishesBean;
import me.yobol.greentable.service.DishesService;

/**
 * @author Yobol
 *
 */
public class DeleteDishesServlet extends HttpServlet {

	private static final long serialVersionUID = 3773224249151432391L;

	public DeleteDishesServlet() {
		super();
	}

	public void init() throws ServletException {
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int dishesId = (request.getParameter("dishesId") == null) ? -1
				: Integer.parseInt(request.getParameter("dishesId"));
		int dishesOwner = (request.getParameter("dishesOwner") == null) ? -1 : Integer
				.parseInt(request.getParameter("dishesOwner"));
		
		if(DishesService.deleteDishes(dishesId)){
			List<DishesBean> dsByOwner = DishesService
					.getDishesByOwnerId(dishesOwner);
			request.getSession().setAttribute("dishes_list", dsByOwner);
			String forwardUrl = "/pages/dishes_list.jsp";
			RequestDispatcher rd = request.getRequestDispatcher(forwardUrl);
			rd.forward(request, response);
		}
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

}
